1609D - Social Network - CodeForces Solution


dsu graphs greedy implementation trees *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff              first
#define ss              second
#define int             long long
#define uniq(v)         (v).erase(unique(all(v)),(v).end())
#define pb              push_back
#define all(v)          v.begin(),v.end()
#define pii             pair<int,int>
#define mem1(a)         memset(a,-1,sizeof(a))
#define mem0(a)         memset(a,0,sizeof(a))
#define vi              vector<int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define rep(i,a,b)      for(int i=a;i<b;i++)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T, typename T1>T amax(T &a, T1 b) {if (b > a)a = b; return a;}
template<typename T, typename T1>T amin(T &a, T1 b) {if (b < a)a = b; return a;}
template<typename T>
using pbds = tree<T , null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update>;
const int N = 1e5 + 5;
int mxSize;
int parent[N], siz[N];
int findParent(int i)
{
    if (i == parent[i])
        return i;
    return parent[i] = findParent(parent[i]);
}
void unionNodes(int a, int b)
{
    int parent_a = findParent(a), parent_b = findParent(b);
    if (parent_a == parent_b)
        return;
    if (siz[parent_a] >= siz[parent_b]) {
        swap(parent_a, parent_b);
    }
    siz[parent_b] += siz[parent_a];
    parent[parent_a] = parent_b;
    return;
}
void cleardsu(int n) {
    mxSize = 0;
    for (int i = 0; i <= n; i++) {
        parent[i] = i;
        siz[i] = 1;
    }
}


void solve() {
    int n, d; cin >> n >> d;

    cleardsu(n);

    int not_connected = 1;

    int disjoint_sets = n;

    // we need a data structure to store the sorted sizes of disjoint sets

    // compute the suffix sum effectively

    multiset<int, greater<int>>st;
    for (int i = 1; i <= n; i++) {
        st.insert(1);
    }

    for (int i = 0; i < d; i++) {
        int a, b; cin >> a >> b;
        int xx = findParent(a);
        int yy = findParent(b);
        if (xx == yy) {
            // if there exist some relation not still connected we can connect them
            not_connected++;
        }
        else {
            // remove two sizes and add one size
            st.erase(st.find(siz[xx]));
            st.erase(st.find(siz[yy]));

            st.insert(siz[xx] + siz[yy]);
            unionNodes(a, b);

            disjoint_sets--;
        }
        int x = findParent(a);
        // find the sum of top not_connected elements
        int cnt = 0;
        int sum = 0;
        for (auto x : st) {
            sum += x;
            cnt++;
            if (cnt == not_connected) {
                break;
            }
        }


        amax(mxSize, siz[x]);
        // out of these not connected how many we can fulfill

        // find out the number of disjoint sets in dsu


        // connect top not connected sets


        cout << sum - 1 << endl;
    }


}


int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#ifdef SIEVE
    sieve();
#endif
#ifdef NCR
    init();
#endif
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;

}


Comments

Submit
0 Comments
More Questions

1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad